﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain;
using Microsoft.Xna.Framework;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;

namespace Brain.Farseer
{
    public class FarseerPhysicsManager2D : IPhysics2DManager
    {
        public bool ShouldUpdate { get; set; }

        public World FarseerWorld;

        private Vector2 defaultGravity = new Vector2(0, 20);
        public Vector2 Gravity
        {
            get { return FarseerWorld.Gravity; }
            set
            {
                FarseerWorld.Gravity = value;
            }
        }

        /// <summary>
        /// Farseer expects objects to be scaled to MKS (meters, kilos, seconds)
        /// 1 meters equals 64 pixels here
        /// (Objects should be scaled to be between 0.1 and 10 meters in size)
        /// </summary>
        public float MeterInPixels = 64f;

        public FarseerPhysicsManager2D()
        {
            ShouldUpdate = true;

            FarseerWorld = new World(defaultGravity);
        }

        public void ChangedState(IPhysicsSprite sprite, SpritePhysics lastState)
        {
            if (sprite.Physics == SpritePhysics.NoPhysics)
            {
                if (sprite.PhysicsObject != null &&
                    sprite.PhysicsObject is Body)
                {
                    Body space = sprite.PhysicsObject as Body;
                    this.FarseerWorld.RemoveBody(space);
                }
            }
            else
            {
                if (sprite.Physics == SpritePhysics.Rectangle)
                {
                    PhysicsObject_Rectangle rect = new PhysicsObject_Rectangle(FarseerWorld,
                        sprite.BoundsRectangle.Width / MeterInPixels,
                        sprite.BoundsRectangle.Height / MeterInPixels, 1f);
                    rect.Position = new Vector2(sprite.BoundsRectangle.X, sprite.BoundsRectangle.Y) +
                        new Vector2(0, sprite.BoundsRectangle.Height);
                    sprite.PhysicsObject = rect;
                }
                else if (sprite.Physics == SpritePhysics.Circle)
                {
                    //Body body = BodyFactory.CreateCircle(FarseerWorld,
                      //  sprite.BoundsRectangle.Width / (2f * MeterInPixels), 1f);
                    //sprite.PhysicsObject = body;
                }
                else if (sprite.Physics == SpritePhysics.PerPixel)
                {
                    throw new NotSupportedException();
                }
                else if (sprite.Physics == SpritePhysics.PhysicsUserHandled)
                {
                }
            }
        }

        public void Update()
        {
            FarseerWorld.Step((float)Engine.Instance.GameTime.ElapsedGameTime.TotalSeconds);
        }

        public void Reset()
        {
        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }


        public bool SupportChangeSizeAfterCreation
        {
            get { return false; }
        }


        public void DeserializedAll()
        {
            throw new NotImplementedException();
        }
    }
}
